package com.tbit.uqbike.service.business.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.tbit.uqbike.object.pojo.BusDesConfig;
import com.tbit.uqbike.service.baseAbstract.SynchronousService;
import com.tbit.uqbike.webmanager.dao.core.BusDesConfigDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 同步购买须知配置
 * @author <a href="mailto:liu.haoxiang@tbit.com.cn">BUGXiang</a>
 * @version 1.0
 * @since 2025-2-24
 */
@Service("purchaseInstructionsService")
@Slf4j
public class PurchaseInstructionsServiceImpl extends SynchronousService {

    @Resource
    private BusDesConfigDao busDesConfigDao;


    @Override
    protected List<?> queryConfig(Integer accountId) {
        List<BusDesConfig> busDesConfigs = busDesConfigDao.queryById(accountId);
        if (CollectionUtil.isNotEmpty(busDesConfigs)) {
            return busDesConfigs;
        } else {
            return Collections.emptyList();
        }
    }

    @Override
    protected void deleteConfig(Integer accountId) {
        List<BusDesConfig> busDesConfigs = busDesConfigDao.queryById(accountId);
        if (CollectionUtil.isNotEmpty(busDesConfigs)) {
           log.info("删除购买须知配置:{}", JSON.toJSONString(busDesConfigs));
           busDesConfigDao.deleteByAccountId(accountId);
        }
    }

    @Override
    protected void insertConfig(List<?> objects, Integer accountId) {
        if (CollectionUtil.isEmpty(objects)) {
            return;
        }
        List<BusDesConfig> busDesConfigList = (List<BusDesConfig>) objects;
        busDesConfigList.forEach(BusDesConfig -> BusDesConfig.setAccountId(accountId));
        busDesConfigDao.batchAdd(busDesConfigList);
    }
}
